﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SoundMatcher.bean_classes;
using System.Data;
using NAligner;
using NAligner.formats;

namespace SoundMatcher
{
    class SoundMatcher
    {

        public List<Song> GetBestFit(String sequenceOfQuery)
        {
            
            List<Song> songCollection = getSongs();

            Sequence s1 = new Sequence();
            s1.AAList = sequenceOfQuery;

            foreach (Song s in songCollection)
            {                 
                Sequence s2 = new Sequence();
                s2.AAList = s.Sequence;

                Alignment alignment = SmithWatermanGotoh.Align(s1, s2, Matrix.Load("BLOSUM50"), 1.5f, 0f);

                s.Accuracy = alignment.Similarity;
                s.Gaps = alignment.Gaps;
           
            }
            SimilarityComparer simCom = new SimilarityComparer();
            GapsComparer gapCom = new GapsComparer();
            OverallValueComparer overallCom = new OverallValueComparer();

///////sorting songs accroding to the no of similaritie and set the rank////////////////////////////
            songCollection.Sort(simCom);

            int a = 1;
            foreach (Song s in songCollection)
            {
                s.SimilarityRank = a++;
            }
///////sorting songs accroding to the no of gaps and set the rank////////////////////////////
            //songCollection.Sort(gapCom);
            //a = 1;
            //foreach (Song s in songCollection)
            //{
            //    s.GapsRank = a++;
            //}
///////calculating the overall value/////////////////////////////
            //foreach (Song s in songCollection)
            //{
            //    s.OverallValue = s.SimilarityRank + s.GapsRank;
            //}
///////sorting songs accroding to the ovearllvalue and set the rank////////////////////////////

            //songCollection.Sort(overallCom);

            //a = 1;
            //foreach (Song s in songCollection)
            //{
            //    s.OverallRank = a++;
            //}

            return songCollection;
        }


        public List<Song> getSongs()
        {
            string query = "SELECT  sequence, id, title FROM song";
            DataTable songs = DB.GetDataTable(query);
            DataTableReader tableReader = songs.CreateDataReader();

            List<Song> songCollection = new List<Song>();
            while (tableReader.Read())
            {
                Song song = new Song();
                song.ID = Convert.ToInt32(tableReader["id"]);
                song.Title = Convert.ToString(tableReader["title"]);
                song.Sequence = Convert.ToString(tableReader["sequence"]);


                songCollection.Add(song);

            }
            return songCollection;
        }


    }
}
